//
//  SettingController.m
//  JF_New_DYKT
//
//  Created by 陈雷 on 2017/8/30.
//  Copyright © 2017年 陈雷. All rights reserved.
//

#import "SettingController.h"
#import "JFSettingCell.h"
#import "SDImageCache.h"
#import "SecondRegisterController.h"

@interface SettingController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tbv;

@property (nonatomic,strong) NSArray *rightContentArr;
@property (nonatomic,strong) NSArray *leftTitleArr;
@property (nonatomic,strong) UIView *footV;
@property (nonatomic,copy) NSString *customerServicePhoneNum;//客服电话

@property (nonatomic,strong) UISwitch *messageSwitch;
@property (nonatomic,strong) UISwitch *liveSwitch;

@end

@implementation SettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self baseConfig];
    self.leftTitleArr = @[@[@"找回密码",@"更换手机号",@"绑定微信",@"绑定QQ"],@[@"消息提醒",@"开播提醒"],@[@"清除缓存"],@[@"给我评分",@"客服热线"]];
    
    self.customerServicePhoneNum = @"4009008835";
    
}

- (void)baseConfig{
    
    self.title = @"设置";
    
    self.tbv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, KSCreenHeight - 64) style:UITableViewStyleGrouped];
    self.tbv.delegate = self;
    self.tbv.dataSource = self;
    [self.view addSubview:self.tbv];
    
    [self.tbv registerNib:[UINib nibWithNibName:@"JFSettingCell" bundle:nil] forCellReuseIdentifier:@"JFSettingCell"];
    self.tbv.separatorColor = [UIColor colorWithHexString:@"eeeeee"];
    self.tbv.separatorInset = UIEdgeInsetsZero;
    self.tbv.backgroundColor = [UIColor colorWithHexString:@"f6f6f6"];
    
    self.tbv.tableFooterView = self.footV;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return .001;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *arr = self.leftTitleArr[section];
    return arr.count;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.leftTitleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JFSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JFSettingCell" forIndexPath:indexPath];
    cell.leftTitleLb.text = self.leftTitleArr[indexPath.section][indexPath.row];
    
    cell.rightArrowImv.hidden = NO;
    
    if (indexPath.section == 1) {
        cell.rightArrowImv.hidden = YES;
        if (indexPath.row == 0) {
            [cell addSubview:self.messageSwitch];
        }else{
            [cell addSubview:self.liveSwitch];
        }
    }
    
    if (indexPath.section == 2) {
        cell.rightArrowImv.hidden = YES;
        float cacheSize = [SDImageCache sharedImageCache].getSize;
        cell.rightContentLb.text = [NSString stringWithFormat:@"%.2lfM",cacheSize/1024/1024];
        cell.rightContentLb.tag = 1000;
    }
    
    if (indexPath.section == 3 && indexPath.row == 1) {
        cell.rightContentLb.text = self.customerServicePhoneNum;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            SecondRegisterController *firstVc = [[SecondRegisterController alloc]init];
            firstVc.fromWhere = @"forgetPsd";
            BaseNavController *registerNav = [[BaseNavController alloc]initWithRootViewController:firstVc];
            [self presentViewController:registerNav animated:YES completion:nil];

        }
    }else if (indexPath.section == 1){
    

        
        
    }else if (indexPath.section == 2){
        
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            UILabel *memoryLb = [self.tbv viewWithTag:1000];
            memoryLb.text = @"0.00M";
        }];
        
    }else if (indexPath.section == 3){
        if (indexPath.row == 0) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=1158625344&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"]];
        }else{
            NSString *phoneNum=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.customerServicePhoneNum];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNum]];
            
        }
    }
    
}


- (UIView *)footV{

    if (!_footV) {
        _footV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, 100)];
        UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        logoutBtn.frame = CGRectMake(15, 25, KSCreenWidth - 30, 40);
        [logoutBtn setBackgroundColor:[UIColor colorWithHexString:@"ffb259"]];
        [logoutBtn addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
        logoutBtn.layer.cornerRadius = 20;
        logoutBtn.layer.masksToBounds = YES;
        [logoutBtn setTitle:@"退出" forState:UIControlStateNormal];
        logoutBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_footV addSubview:logoutBtn];
    }
    return _footV;
}

- (void)logoutAction{

    
    [JKToast showWithText:@"您已退出登录！"];
    [UserDefaults removeObjectForKey:@"UID"];
    LoginController *loginVc = [[LoginController alloc]init];
    [self presentViewController:loginVc animated:YES completion:nil];
    
//    NSString *urlStr = [NSString stringWithFormat:@"%@user/User/logout",kDomainName];
//    
//    
//    BaseRequest *request =  [BaseRequest sharedBaseRequest];
//    [request post:urlStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
//        if ([responseObject[@"code"] isEqualToString:@"1"] ) {
//            
//
//            
//        }else{
//            
//            [JKToast showWithText:responseObject[@"message"]];
//        }
//        
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        
//    }];
}


- (UISwitch *)messageSwitch{
    if (!_messageSwitch) {
        _messageSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(KSCreenWidth - 65, 10, 50, 50)];
    }
    return _messageSwitch;
}


- (UISwitch *)liveSwitch{
    if (!_liveSwitch) {
        _liveSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(KSCreenWidth - 65, 10, 50, 50)];
    }
    return _liveSwitch;
}

@end
